package com.mischa.atc;

public class Coordinate 
{
	float _x;
	float _y;
	
	public static final Coordinate Origin = new Coordinate(0f, 0f);
	
	public Coordinate()
	{
		_x = 0;
		_y = 0;
	}
	
	public Coordinate(float x, float y)
	{
		_x = x;
		_y = y;
	}
	
	public float getX() 
	{
		return _x;
	}
	
	public float getY()
	{
		return _y;
	}
	
	public void setX(float x)
	{
		_x = x;
	}
	
	public void setY(float y)
	{
		_y = y;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Float.floatToIntBits(_x);
		result = prime * result + Float.floatToIntBits(_y);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Coordinate other = (Coordinate) obj;
		if (_x != other._x)
			return false;
		if (_y != other._y)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return String.format("(%f, %f)", _x, _y);
	}

	public Heading headingTo(Coordinate other) {
		float deltaX = other._x - this._x;
		float deltaY = other._y - this._y;
		
		// Note: atan2 has angle 0 at compass direction 90
		float angle = (float)Math.toDegrees(Math.atan2(deltaX, deltaY));
		return new Heading(angle);
	}
	
	public Heading headingFromOrigin() {
		return Coordinate.Origin.headingTo(this);
	}
	
	public float distanceTo(Coordinate other) {
		float deltaX = other._x - this._x;
		float deltaY = other._y - this._y;
		
		return (float) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
	}

	public float distanceFromOrigin() {
		return distanceTo(Coordinate.Origin);
	}

}
